package com.ray.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ray.bean.CompanyBean;
import com.ray.bean.DatatablesViewPageBean;
import com.ray.bean.WarehouseInOutDetailBean;
import com.ray.exception.ServiceException;
import com.ray.pojo.OrderCostGlassesPojo;
import com.ray.service.CompanyService;
import com.ray.service.OrderCostGlassesService;
import com.ray.util.ExcelExportUtil;
import com.ray.util.RequestUtil;
import com.ray.util.ResponseUtil;

/**
 * 订单眼镜成品Controller
* <p>Title: OrderCastGlassesController</p>  
* <p>Description: </p>  
* @author 李斌   
* @date 2018年8月3日
 */
@Controller
@RequestMapping("/orderCostFlasses")
public class OrderCostGlassesController {
	
	@Autowired
	OrderCostGlassesService orderCostGlassesService;
	@Autowired
	CompanyService companyService;
	/**
	 * 获取每副眼镜的成本
	* @date 2018年8月4日 上午9:16:49 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */ 
	@RequestMapping("/getCostClassesDetail")
	@ResponseBody
	public DatatablesViewPageBean<OrderCostGlassesPojo> getCostClassesDetail(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		CompanyBean companyBean = companyService.getCompany(map);
		if(companyBean.getType()==2) {//设备公司获取设备的计算方式
			return orderCostGlassesService.getMachineCostGlasses(map);
		}else {
			return orderCostGlassesService.getOrderCostGlasses(map);
		}
	}
	
	
	/**
	 * 订单成本汇总
	 * @param request
	 * @param response
	 * @throws NoSuchFieldException
	 * @throws SecurityException
	 * @throws IllegalArgumentException
	 * @throws IllegalAccessException
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/exportExcelOrderCostClasses",method=RequestMethod.GET)
	public void exportExcelOrderCostClasses(HttpServletRequest request,HttpServletResponse response) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, IOException{
		 Map<String, Object> map = RequestUtil.getRequestAllParam(request);
		 map.put("start", null);
		 map.put("length", null);
		 DatatablesViewPageBean<OrderCostGlassesPojo> datatablesViewPageBean=null;
		 CompanyBean companyBean = companyService.getCompany(map);
		 if(companyBean.getType()==2) {//设备公司获取设备的计算方式
			 datatablesViewPageBean =  orderCostGlassesService.getMachineCostGlasses(map);
		 }else {
			 datatablesViewPageBean =  orderCostGlassesService.getOrderCostGlasses(map);
		 }
		 List<OrderCostGlassesPojo> list = datatablesViewPageBean.getReturnData();
		 for(OrderCostGlassesPojo dd:list) {
			 if("-1".equals(dd.getLineNo())) {
				 dd.setLineNo("0");
			 }
			 if(dd.getYuCost()==0.0) {
				 dd.setExportYuCost("未设置");
			 }else {
				 dd.setExportYuCost(dd.getYuCost()+"");
			 }
			 if(dd.getPeopleCost()==0.0) {
				 dd.setExportPeopleCost("未生产");
			 }else {
				 dd.setExportPeopleCost(dd.getPeopleCost()+"");
			 }
		 }
		 String [] fields  = {"companyOrderId","factoryId","customerId","lineNo","feedTotal","feedCount","orderTotal","glassesMoney","glassesOrderMoney","exportYuCost","exportPeopleCost","managerCost"};
		 String [] heads = {"订单编号","工厂型号","客户型号","订单序号","订单投料数","物料用料数","订单数","投料成本","订单成本","预计人工","实际人工","管理成本"};
		 String tempFolder=request.getServletContext().getRealPath("/")+"tempExcelFolder";
		 File excelFile=ExcelExportUtil.createExcelFile(list,tempFolder,heads,fields);
		 ResponseUtil.downloadfile(response, excelFile, "订单成本汇总.xlsx", true);
	}
	
	/**
	 * 获取订单信息list
	* @date 2018年8月8日 下午1:16:29 
	* @author 李斌
	* @return Map<String,Object> 
	* @throws
	 */
	@RequestMapping("/getOrderIdList")
	@ResponseBody
	public Map<String,Object> getOrderIdList(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderCostGlassesService.getOrderIdList(map));
	}
	
	@RequestMapping("/getOrderLineDetailList")
	@ResponseBody 
	public Map<String,Object> getOrderLineDetailList(HttpServletRequest request){
		Map<String,Object> map = RequestUtil.getRequestAllParam(request);
		return ResponseUtil.ResponseResult(orderCostGlassesService.getOrderLineDetailList(map));
	}
}
